package com.meilai.project.controller.mini;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.personnel.org.DepartmentDTO;
import com.meilai.project.entity.personnel.org.Department;
import com.meilai.project.service.personnel.org.DepartmentService;
import com.meilai.project.util.ThreadLocalUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("小程序-部门")
@RestController
@RequestMapping("api/mini/department")
public class MiniDepartmentController {

    @Autowired
    private DepartmentService service;

    @GetMapping("list")
    @ApiOperation(value = "列表")
    public CommonResult list(@RequestParam(name = "name", required = false) String name,
                             @RequestParam(name = "status", required = false) Integer status) {
        return new CommonResult().success(service.selectPageList(name, status));
    }

    @PutMapping("status")
    @ApiOperation(value = "更改状态")
    public CommonResult changeStatus(@RequestParam(value = "ids[]") Long ids, Integer status) {
        if (service.changeStatus(ids, status)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PutMapping("accounting")
    @ApiOperation(value = "更改独立核算")
    public CommonResult changeAccounting(@RequestParam(value = "ids[]") Long ids, boolean accounting) {
        if (service.changeAccounting(ids, accounting)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PutMapping("delete")
    @ApiOperation(value = "删除")
    public CommonResult deleted(@RequestParam(value = "ids[]") List<Long> ids) {
        if (service.del(ids)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    public CommonResult add(@RequestBody DepartmentDTO dto) {
        if (service.addOne(dto)) {
            return new CommonResult().success("新增成功");
        } else {
            return new CommonResult().failed("新增失败");
        }
    }

    @PostMapping("edit")
    @ApiOperation(value = "修改")
    public CommonResult edit(@RequestBody DepartmentDTO dto) {
        if (service.editOne(dto)) {
            return new CommonResult().success("修改成功");
        } else {
            return new CommonResult().failed("修改失败");
        }
    }

    @GetMapping("list_by_user")
    public CommonResult ListByUser() {
        return new CommonResult().success(service.getDepartmentsByUserId(ThreadLocalUserUtil.get().getId()));
    }

    @GetMapping("get_company")
    @ApiOperation(value = "获取除顶级部门外所有独立核算的部门", notes = "获取除顶级部门外所有独立核算的部门", response = Department.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    public CommonResult getCompany() {
        return new CommonResult().success(service.getCompany());
    }

    @GetMapping("getDeptCompleteNameById")
    @ApiOperation(value = "根据部门ID获取部门全名称（根部门除外）", notes = "根据部门ID获取部门名称（根部门除外）", response = String.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    public CommonResult getDeptCompleteNameById(@RequestParam("id") Long id) {
        return new CommonResult().success(service.getDeptCompleteNameById(id));
    }
}
